package Multithreading.Wait_and_Notify001;

/**
 * ClassName:Foodie
 * Package:Multithreading.Wait_and_Notify
 * Description:
 *
 * @Author:ZhangXin
 * @Create2023/3/5-21:47
 * @Version:v1.0
 */
public class Foodie extends Thread{
    @Override
    public void run() {
        /*1.循环
         * 2.同步代码块
         * 3.判断共享数据是否到了末尾(到了末尾)
         * 4.判断共享数据没有到末尾(没有到末尾,执行核心逻辑)*/
        while (true) {
            synchronized (Dest.lock) {
                if (Dest.count == 0) {
                    break;
                } else {
                    //先要判断桌子上是否有面条
                    if (Dest.foodFlag == 0) {
                        //如果没有,就等待
                        try {
                            Dest.lock.wait();//让当前线程和锁进行绑定
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } else {
                        //把吃的总数-1
                        Dest.count--;
                        //如果有,开吃
                        System.out.println("吃货在吃面条,还能吃" + Dest.count + "碗面条!!");
                        //吃完之后唤醒厨师继续做
                        Dest.lock.notifyAll();//唤醒跟这把锁绑定的所有线程

                        //修改桌子的状态
                        Dest.foodFlag = 0;
                    }

                }
            }
        }
    }
}
